from time import sleep
from study import study
import re

page_login = "http://main.zgjiaoyan.com/passport/login/"
page_corse = [
    # "https://gzxkc.zgjiaoyan.com/train/guide/course/list?projectId=5405054851245858838&role=100&toolId=5422979021082517508&stageId=5422973179926863876&barId=&examineSubstanceRole=MEMBER",
    "https://gzxkc.zgjiaoyan.com/train/guide/course/list?projectId=5405054851245858838&role=100&toolId=5422987507937894417&stageId=5422973592243855363&barId=&examineSubstanceRole=MEMBER"
    ]
jy = study()
jy.driver.get(page_login)
sleep(5)
jy.clickElement("//span[text()='密码登录']")
jy.inputText("//input[@id='username']", '15399900725')
jy.inputText("//input[@id='password']", 'oditszXC102298')
jy.driver.execute_script("doLogin()")
sleep(5)
for page in page_corse:
    jy.driver.get(page)
    sleep(5)
    jy.clickElement("//span[text()='必修课程']")
    sleep(5)
    corses = jy.getElements("//div[@class='list']/div/div/div[1]/span[@class='img']")
    print("一共有%d个视频" %(len(corses)))
    for corse in corses:
        corse.click()
        sleep(5)
        jy.driver.switch_to.window(jy.driver.window_handles[1])
        print("%s  开始听课--%s" % (jy.time, jy.getElement("//h2/span").text))

        while True:
            el_next = jy.getElement("//div[@class='ended-mask']/div[2]/p[@class='next']")
            el_replay = jy.getElement("//div[@class='ended-mask']/div[2]/p[@class='replay']")
            el_continue = jy.getElement("//div[@class='alarmClock-wrapper']")
            el_refuse = jy.getElement("//div[@class='questionnaire-wrapper']/div/div[2]/button[1]")
            el_active_doc = jy.getElement("//li[@class='res-item active']/div/i[contains(@class, 'icon-doc')]")
            el_error_tip = jy.getElement("//div[@class='vcp-error-tips']")
            el_bigplay_is_display = jy.getFakeElementAfterDisplay('.vcp-bigplay')
            el_bigplay = jy.getElement("//div[@class='vcp-bigplay']")
            el_id_app = jy.getElement("//div[@id='app']")

            # 某一小节已学完,点击下一节
            if el_next != None:
                print("%s  %s" % (jy.time, el_next.text))
                el_next.click()
                sleep(5)
                continue
            # 本视频全部学完
            if el_next == None and el_replay != None:
                jy.driver.close()
                jy.driver.switch_to.window(jy.driver.window_handles[0])
                print("%s  %s" % (jy.time, "本视频已学完"))
                break
            # 继续计时的按键
            if el_continue != None:
                el_continue.click()
                print("%s  %s" % (jy.time, "点击了继续计时"))
                continue
            # 拒绝评价
            if el_refuse != None:
                el_refuse.click()
                print("%s  %s" % (jy.time, "点击了拒绝评价"))
                continue
            # 如果是文档了
            if el_active_doc != None:
                print("%s  %s" % (jy.time, "这个已经是文档了"))
                jy.driver.close()
                jy.driver.switch_to.window(jy.driver.window_handles[0])
                break
            # 如果遇到网络问题
            if el_error_tip != None:
                print("%s  %s" % (jy.time, "网络有问题了,尚在视频页面, 刷新中"))
                jy.driver.refresh()
                sleep(10)
                continue
            if el_id_app == None:
                print("%s  %s" % (jy.time, "网络有问题了,没有页面,刷新中..."))
                jy.driver.refresh()
                sleep(10)
                continue
            # 如果暂停了,要求没有显示重新播放,因为播放完一段
            if el_bigplay_is_display == True:
                print("%s  %s" % (jy.time, "被暂停了,帮你点击播放"))
                el_bigplay.click()
                sleep(5)
                continue

            # 过一段时间再去检查
            sleep(5)

